<?php 
session_start();
include("db_connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Basciautosales store the place where you can buy trade set build your own dream cars at its your ways" />
<meta name="description" content="Basciautosales store the place where you can buy trade set build your own dream cars at its your ways" />


<?php include("include/jquery.php");

include("shop/functions.php");
include("class/objName.php");
$msg= "";
$pid ="";
$q="";
if(isset($_REQUEST['command'])){

		if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
		remove_product($_REQUEST['pid']);
		}
		else if($_REQUEST['command']=='clear'){
			unset($_SESSION['cart']);
		}
		else if($_REQUEST['command']=='update'){
			$max=count($_SESSION['cart']);
			for($i=0;$i<$max;$i++){
				$pid=$_SESSION['cart'][$i]['productid'];
				$q=intval($_REQUEST['product'.$pid]);
					
					if($q>0 && $q<=999){
						$_SESSION['cart'][$i]['qty']=$q;
						
					}
					else{
						$msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
					}
					
				
				
				
				
			}
		}
	
}
	

?>
<script language="javascript">
var loader = '<div style="padding:10px; margin:auto"><img src="images/loading.gif" alt="Please Wait"/>loading.....</div>';

function del(pid){
		if(confirm('Do you really mean to delete this item')){
			document.form1.pid.value=pid;
			document.form1.command.value='delete';
			document.form1.submit();
			return true;
		}
	}
function clear_cart(){
		if(confirm('This will empty your shopping cart, continue?')){
			document.form1.command.value='clear';
			document.form1.submit();
		}
}

function update_cart(){
var qtyarray = new Array();
$("#placeOrderTable tr td").find("input:text").each(function(e){
	var pid = $(this).attr('name');
	var qty = $(this).val();
	var pid2 = pid.split('product');
	//alert(qty);
	qtyarray[e+e] = qty+pid2;
	

});
	update_mycart(qtyarray);

}



function update_mycart(qtycheck){
	$.ajax({
		url:"scripts/qtycheck.php?qtyid="+qtycheck,
		success:function(data){
		//alert(data);
			if(data=='true'){
				alert("Please check our stock qty it seems you exceed to our limit stock.");
				return false;
			}
			else{
			document.form1.command.value='update';
			document.form1.submit();
			}
				
		}		
	
	});
	
}


function deleteallcart(){

	var command = "deleteall";
	if(confirm("Do you want to clear your cart?")){
	$.ajax({
				url:"shop/cart.php?command="+command,
				success:function(data){
				
				location.reload();
				
				}		
			})
	}
}

function redirect(){
	
	var command = "sessioncheck";
	$.ajax({
	url:"scripts/issessionset.php?command="+command,
	success:function(data){
		if(data == 1){
			
			window.location.href = "paypal/mybill.php"; 
		}
		else{
			$("#msgbox").html("You must log in first!");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					login: function() {
						$( this ).dialog( "close" );
					},
					
				},
			
			});
		}
		
		
	}
	});
	
}

</script>
</head>
<body>
<div id="msgbox"></div>
<div id="templatemo_wrapper">
	<!--header-->
		<?php include("include/header.php");?>
	<!--header-->
    
    <div id="templatemo_main" style="background-color:black; color:white">
   		<div id="placeorder_main">
			<h3 style="color:#fff; padding:5px;">My Cart Place Order</h3>
			<div id="placeShop">
				<div id="placeOrderHolder">
							<?php
								$display = "";
								
								if(!isset($_SESSION['cart'])){
									
									$display = "Your cart is empty";
									echo $display;
									die();
								}
								if(count($_SESSION['cart']) <= 0 ){
									$display = "Your cart is empty";
									echo $display;
									die();
								}
								
								if(is_array($_SESSION['cart'])){
										
										$max = count($_SESSION['cart']);
										if($max>0){
											$display = '<form name="form1" method="post" >
											<input type="hidden" name="pid" />
											<input type="hidden" name="command" />
											<table name="placeOrderTable" id="placeOrderTable" border="" width="100%">
											<tr>
											<th>Name</th>
											<th>Categories</th>
											<th>Condition</th>
											<th>Brand</th>
											<th>Model</th>
											<th>Parts</th>
											<th>Price</th>
											<th>Stock Qty</th>
											<th>Qty</th>
											<th>Amount</th>
											<th>x</th>
										</tr>';
											;
											for($i=0; $i<$max; $i++){
												$pid = $_SESSION['cart'][$i]['productid'];
												$qty = $_SESSION['cart'][$i]['qty'];
												//echo $pid . "-" . $qty ."<>";
												$display .= displaycart($pid,$qty);
											
											}
											$display .="</tr> </table></form>";
											//$display .= '' ;
											echo $display;
										}
										else{
											$display = "You dont have any item in your cart";
										}
									}
								

								function displaycart($pid,$qty){
										$result=mysql_query("select * from tblswaping where id=$pid") or die("select name from products where id=$pid"."<br/><br/>".mysql_error());
										$display ="";
										$display .= "<tr>";
										//<div id="placeoderimg"><img id="placeoderimg"  src="images/vehicles/'.$row['filename'].'"/></div><br/>
										while($row = mysql_fetch_array($result)){
											
											$display .=	'<td valign="top" > <label> '. $row['name'] .' </label></td>';
											$display .= '<td valign="top" >'.$row['categories'].'</td>';
											$display .= '<td valign="top" >'.$row['conditions'].'</td>';
											$display .= '<td valign="top" >'.$row['brand'].'</td>';
											$display .= '<td valign="top" >'.$row['model'].'</td>';
											$display .= '<td valign="top" >'.$row['cparts'].'</td>';
											$display .=	'<td valign="top" > '. $row['price'] .' </td>  ';
											$display .=	'<td valign="top" > '. $row['quantity'] .' </td>  ';
											$display .=	' <td valign="top" ><input type="text" name="product'.$pid.'" id="qty" value="'.$qty.'" maxlength="3" size="2" "/></td>';
											$display .=	'<td valign="top" > '. $row['price'] * $qty .' </td>  ';	
											$display .=	'<td valign="top" ><a href="#" id="btndelete" name="'.$row['id'].'" onclick="del('.$pid.')" >x</a></td>';
											
											
										}
										
										return $display	;
								}

							?>
				</div>
			</div>
			<div id="placeShopBottom">	
				<div id="placeLeft">
					<?php echo $msg;?>
					</br>
					<label >Total:</label>
					<label id="lbltotal">P <?php echo get_order_total();?></label>&nbsp&nbsp&nbsp
					<input type="hidden" value="<?php $_SESSION['totalamount'] = get_order_total();?>" id="totalAmount" />
					<input type="button" value="Update Cart" onclick="update_cart()">
					<input type="button" value="Delete All" onclick="deleteallcart()">
				</div>
				
				<div id="placeRight">
					<button id="btnorder" value="order" id="order" onclick="redirect()" >order</button> 
									
				</div>		
			</div>	
		</div><!--End of placeorder_menu-->
	

	
		       
    <div class="cleaner"></div>
    </div> <!-- END of templatemo_main -->
    
    <!--footer-->
	<?php include("include/footer.php");?>
    
</div> <!-- END of templatemo_wrapper -->

</body>
</html>